﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class CodeBlockCommand : SpeechCommand
    {
        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            return new GrammarBuilder("code block");
        }

        public override string Name
        {
            get { return "Code block"; }
        }

        public override string Description
        {
            get { return Name; }
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            return spokenForm == "code block";
        }

        public override string GetAlternativesFeedback(string recognizedText, SemanticValue semValue)
        {
            return "Code block";
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            string selectedText = InputHandler.ReadSelectedText();
            if (selectedText != null)
            {
                selectedText = selectedText.Trim();

                callingMode.CurrentGuiHandler.SetPrimaryFeedback("Code block");
                if (selectedText.Length > 0)
                {
                    callingMode.CurrentOutputHandler.SendKeys("{END}{ENTER}{{}{ENTER}{}}{UP}{ENTER}");
                    return "^z^z^z^z^z^z^z";
                }
                else
                {
                    callingMode.CurrentOutputHandler.SendKeys("{{}{ENTER}{}}{UP}{ENTER}");
                    return "^z^z^z^z^z^z";
                }
            }
            else
            {
                callingMode.CurrentGuiHandler.SetErrorFeedback("Cursor not in textfield!");
            }
            return null;
        }
    }
}
